/**
 * To show the placemark and manage surrounded cars(incoming,outgoing, locating),asign a placemark for each.
 */

var counter=0;

var  carManager={
allcars:new Object(),


newCar: function(name,lat,lon,alt,vel,heading,mid){

  var car=new SimpleTruck(name,mid);  
  
  //car.teleportTo(lat,lon,null);  
  return car;
},
//Outside the distance; update the coord. when the car move on.
updateCars:function(list){	
	
  if (list==null)return;    
  var cardatalist=list.split('<BR>');
  var detail=new Array();
  var newList=new Object();
  if (cardatalist==null){
	  
	  //no other cars here, clear 
	  for (var car in carManager.allcars){
		  carManager.allcars[car].selfRemove();
		  //ge.getFeatures().removeChild(carManager.allcars[car]);
		  carManager.allcars[car]=null;
	  }
	  carManager.allcars=new Object();
	  return;
  }else{
	  
	  //some cars here, but some existing may have gone,some are new
  for (var i=0;i< cardatalist.length;i++){
	  detail[i]=cardatalist[i].split(':');	 
	  if (detail[i].length>=8){		  
		  //id,lat,lon,alt,vel complete	
		  
	    if(carManager.allcars[detail[i][1]]==null){
	    	
	    //new car	    	    	
		  newList[detail[i][1]]=carManager.newCar(detail[i][0],eval(detail[i][2]),
				  eval(detail[i][3]),
				  eval(detail[i][4]),eval(detail[i][5]),eval(detail[i][6]),detail[i][7]);
		  
	    }
	  }
  }
  //remove other cars
  for (var car in carManager.allcars){
	  
	  var found=false;
	  for (var i=0;i< cardatalist.length;i++){		  
		  if (detail[i].length>=8 && car==detail[i][1]){
			  found=true;
			//car exist already, only update location
			  newList[car]=carManager.allcars[car];
    		  /*var point = ge.createPoint('');
    			 // KmlPoint(allcars[car].getGeometry());		  
    		  point.setLatitude(eval(detail[i][2]));
    		  point.setLongitude(eval(detail[i][3]));*/
    		  //point.setAltitude(detail[i][3]);
    		 // newList[detail[i][1]].setGeometry(point);    		  
    		  
			  newList[detail[i][1]].teleportTo(eval(detail[i][2]),eval(detail[i][3]),null);			  
			  break;
		  }
	  }
    if(!found){    	
    	//car is gone, remove it from the list        
    	carManager.allcars[car].selfRemove();    	
    }    
  }
  //asign the list
  carManager.allcars=newList;
 }
}
};
